package com.dushiguang.findhoney.Activity;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.dushiguang.findhoney.BaseActivity;
import com.dushiguang.findhoney.BmobDB.BmobMethod;
import com.dushiguang.findhoney.Bean.HoneyInfo;
import com.dushiguang.findhoney.MainActivity_;
import com.dushiguang.findhoney.R;
import com.dushiguang.findhoney.Utils.Timer.TimerUtil;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;


/**
 * 登录界面
 * Created by ex-dushiguang201 on 2015-11-02.
 */
@EActivity(R.layout.activity_login)
public class LoginActivity extends BaseActivity implements View.OnClickListener{
    private HoneyInfo mHoneyInfo;
    private static String pwd = null;
    private String loginName;
    private String loginPwd;
    private BmobMethod mBmobMethod;
    private Context context;
    private TimerUtil timebutton;

    @ViewById(R.id.edit_login_name)
    EditText mLoginName;
    @ViewById(R.id.edit_login_pwd)
    EditText mLoginPwd;

    @ViewById(R.id.button_time)
    Button mTimeButton;
    @ViewById(R.id.btn_login)
    Button mBtnLogin;

    @AfterViews
    void afterViews() {
        context = this;

        timebutton = new TimerUtil(LoginActivity.this, mTimeButton, 60000, 1000);
        mTimeButton.setOnClickListener(this);

        mLoginPwd.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (!mLoginName.getText().toString().isEmpty()) {
                    mBtnLogin.setEnabled(true);
                } else {
                    mBtnLogin.setEnabled(false);
                }
            }
        });

        mLoginName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (!mLoginPwd.getText().toString().isEmpty()) {
                    mBtnLogin.setEnabled(true);
                } else {
                    mBtnLogin.setEnabled(false);
                }
            }
        });

    }

    @Click(R.id.btn_login)
    void BtnLogin() {
        loginName = mLoginName.getText().toString();
        loginPwd = mLoginPwd.getText().toString();
        mBmobMethod = new BmobMethod();
        mBmobMethod.initBmob(this);
        mBmobMethod.FindBmobDb(loginName,loginPwd);

    }

    private void FindBmobDb(){

        BmobQuery<HoneyInfo> query = new BmobQuery<HoneyInfo>();
        query.addWhereEqualTo("honeyLoginName", loginName);

        query.findObjects(this, new FindListener<HoneyInfo>() {
            String pwd = null;
            @Override
            public void onSuccess(List<HoneyInfo> object) {
                pwd = object.get(0).getHoneyLoginPwd();
                LoginActivity.pwd = pwd;
                if(!loginPwd.isEmpty() && loginPwd.equals(pwd)){
                    Toast.makeText(LoginActivity.this,"登录成功 ==" + object.get(0).getHoneyLoginPwd(),Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(LoginActivity.this, MainActivity_.class);
                    startActivity(intent);
                }
            }

            @Override
            public void onError(int code, String msg) {
                Toast.makeText(LoginActivity.this,"失败",Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onClick(View view) {
        if ((R.id.button_time) == view.getId()){
            Log.e("dushiguang","onClick====>>");
            timebutton.start();
        }
    }
}
